package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.ProductCategoryApi;
import com.yoda.mall.model.entity.pms.ProductCategory;
import com.yoda.mall.model.entity.pms.ProductCategoryAttributeRelation;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.ProductAttributeCategoryMapper;
import com.yoda.mall.mysql.mapper.ProductCategoryAttributeRelationMapper;
import com.yoda.mall.mysql.mapper.ProductCategoryMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 12月 18日 0:04
 */
@DubboService
public class ProductCategoryApiImpl implements ProductCategoryApi {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Resource
    private ProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;

    private ProductAttributeCategoryMapper productAttributeCategoryMapper;

    @Override
    public List<ProductCategory> getAll() {
      return productCategoryMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    public PageResult<ProductCategory> getCategoryByPage(Long parentId, Integer page, Integer pageSize) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductCategory::getParentId,parentId);
        Page<ProductCategory> categoryPage = new Page<>(page, pageSize);
         productCategoryMapper.selectPage(categoryPage,wrapper);
         return new PageResult<>(page,pageSize,(int) categoryPage.getTotal(),categoryPage.getRecords());
    }

    @Override
    public ProductCategory getCategoryById(Long id) {
        return productCategoryMapper.selectById(id);
    }

    @Override
    @Transactional//开启事务
    public Integer createCategory(ProductCategory productCategory, List<Long> ids) {
        //0查询当前分类等级
        if (productCategory.getParentId()==0){
            productCategory.setLevel(0);
        }else {
            ProductCategory parent = productCategoryMapper.selectById(productCategory.getParentId());
            productCategory.setLevel(parent.getLevel()+1);
        }
        //1.保存分类
        int count1 = productCategoryMapper.insert(productCategory);
        //2.生成分类属性关系
        List<ProductCategoryAttributeRelation> prs = ids.stream().map(id -> {
            ProductCategoryAttributeRelation pcr = new ProductCategoryAttributeRelation();
            pcr.setProductCategoryId(productCategory.getId());
            pcr.setProductAttributeId(id);
            return pcr;
        }).collect(Collectors.toList());
        //3. 保存生成分类属性关系
      Integer count2 =  productCategoryAttributeRelationMapper.saveBatch(prs);
      return count1+count2;
    }

    @Override
    @Transactional
    public Integer deleteCategoryById(Long id) {
        //删除类别
        int count1 = productCategoryMapper.deleteById(id);
        //删除类别对应的属性关联表
        LambdaQueryWrapper<ProductCategoryAttributeRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductCategoryAttributeRelation::getProductCategoryId,id);
        int count2 = productCategoryAttributeRelationMapper.delete(wrapper);
        return count1+count2;
    }

    @Override
    public Integer updateCategoryById(ProductCategory productCategory, List<Long> ids) {
        //根据id删除分类属性关联表
        LambdaQueryWrapper<ProductCategoryAttributeRelation> relationWrapper = new LambdaQueryWrapper<>();
        relationWrapper.eq(ProductCategoryAttributeRelation::getProductCategoryId,productCategory.getId());
        int count1 = productCategoryAttributeRelationMapper.delete(relationWrapper);
        //根据id更新分类
        int count2 = productCategoryMapper.updateById(productCategory);
        //2.生成分类属性关系
        List<ProductCategoryAttributeRelation> prs = ids.stream().map(id -> {
            ProductCategoryAttributeRelation pcr = new ProductCategoryAttributeRelation();
            pcr.setProductCategoryId(productCategory.getId());
            pcr.setProductAttributeId(id);
            return pcr;
        }).collect(Collectors.toList());
        Integer count3 = productCategoryAttributeRelationMapper.saveBatch(prs);
        return count1+count2+count3;
    }

    @Override
    public Integer changeShowStatus(Integer showStatus, List<Long> ids) {
       return productCategoryMapper.updateShowStatusByBatch(showStatus,ids);
    }

    @Override
    public Integer changeNavStatus(Integer navStatus, List<Long> ids) {
        return productCategoryMapper.updateNavStatusByBatch(navStatus,ids);
    }
}
